package com.servlet;

import com.dao.CardDao;
import com.dao.Impl.CardDaoImpl;
import com.dao.Impl.UserDaoImpl;
import com.dao.UserDao;
import com.entity.Card;
import com.util.MyFilter;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/cardServlet/*")
public class CardServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String url=request.getRequestURI();
        String method=url.substring(url.lastIndexOf("/")+1);
        String target=null;
        switch (method){
            case "moneyAdd":
                target=moneyAdd(request,response);
                break;
            case "alterStatus":
                target=alterStatus(request,response);
                break;
            case "findCardBasic":
                target=findCardBasic(request,response);
                break;
            case "findCardId":
                target=findCardId(request,response);
                break;
            case "insertCard":
                target=insertCard(request,response);
                break;
            default:break;
        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }



    /**
     * 会员充值
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String moneyAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Card card=new Card();
        card.setCardId(Integer.valueOf(request.getParameter("cardId")));
        card.setAmount(Double.parseDouble(request.getParameter("money")));
        CardDao dao=new CardDaoImpl();
        dao.updateAmount(card);
        return null;
    }

    /**
     * 会员卡权限更改
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String alterStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Card card=new Card();
        card.setCardId(Integer.valueOf(request.getParameter("cardId")));
        card.setStatus(Integer.valueOf(request.getParameter("status")));
        CardDao dao=new CardDaoImpl();
        dao.updateStatus(card);
        return null;
    }

    /**
     *根据Id查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String findCardBasic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int cardId= Integer.parseInt(request.getParameter("cardId"));
        CardDao cardDao=new CardDaoImpl();
        List<Map> cardBasic = cardDao.findCardBasic(cardId);
        MyUtil.jsonWriter(response,cardBasic);
        return null;
    }

    /**
     * 查询是否有此ID
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String findCardId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int cardId= Integer.parseInt(request.getParameter("cardId"));
        CardDao dao=new CardDaoImpl();
        int id = dao.findCardId(cardId);
        MyUtil.jsonWriter(response,id);
        return null;
    }

    /**
     * 会员登记中向card表中添加数据
     * @param request
     * @param response
     * @return
     */
    private String insertCard(HttpServletRequest request, HttpServletResponse response) {
        Card card=new Card();
        TransBean.populate(card,request.getParameterMap());
        UserDao udao=new UserDaoImpl();
        card.setUserId(udao.findUserId());
        CardDao cdao=new CardDaoImpl();
        cdao.insertCard(card);
        return null;
    }

}
